<template>
  <div>
    <el-select :loading="isFirstLoad" v-model="name" filterable :placeholder="$t('common.placeholderSelect')" @change="handleChange" @visible-change="visibleChange" clearable>
      <el-option v-for="item in options" :key="item.id" :label="item.nickname" :value="item.id"></el-option>
    </el-select>
  </div>
</template>
<script>
import { memberList } from '@/api/money'
export default {
  data() {
    return {
      name: '',
      timeout: null,
      options: [],
      pageNum: 1,
      loading: false,
      isFirstLoad: true
    }
  },
  props: {
    value: {
      type: String | Number,
      default: ''
    }
  },
  methods: {
    visibleChange(v) {
      if (v && this.options.length == 0) {
        this.getCustomers()
      }
    },
    getCustomers() {
      if (!this.loading) {
        this.loading = true
        memberList({
          pageNum: this.pageNum,
          pageSize: 10
        })
          .then(r => {
            this.name = r.data.name
            this.loading = false
            this.isFirstLoad = false
            if (r.data.list.length > 0) {
              this.pageNum++
              this.options = [...this.options, ...r.data.list]
            }
          })
          .catch(e => {
            this.loading = false
            this.isFirstLoad = false
          })
      }
    },
    handleChange(item) {
      this.$emit('input', item)
      this.$emit('change', item)
    }
    // getStoreById(id) {
    //   if (id != 0) {
    //     api('admin/store/getStoreById', {
    //       id
    //     }).then(r => {
    //       this.name = r.data.name
    //     })
    //   }
    // }
  }
}
</script>
